package test.resource

import com.gitee.ixtf.core.kotlinx.sha256Hex
import com.gitee.ixtf.vertx.kotlinx.path
import io.vertx.core.json.JsonArray
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.FileUpload
import io.vertx.kotlin.core.json.array
import io.vertx.kotlin.core.json.json
import io.vertx.kotlin.core.json.obj
import org.eclipse.microprofile.graphql.GraphQLApi
import org.eclipse.microprofile.graphql.Mutation
import org.eclipse.microprofile.graphql.Name

@GraphQLApi
class MutationResource {
  @Mutation
  suspend fun Upload(@Name("file") fileUpload: FileUpload): JsonObject = json {
    obj(
        "filename" to fileUpload.fileName(),
        "size" to fileUpload.size(),
        "sha256" to fileUpload.path().sha256Hex())
  }

  @Mutation
  suspend fun Uploads(@Name("files") fileUploads: Collection<FileUpload>): JsonArray = json {
    array(
        fileUploads.map { fileUpload ->
          obj(
              "filename" to fileUpload.fileName(),
              "size" to fileUpload.size(),
              "sha256" to fileUpload.path().sha256Hex())
        })
  }

  @Mutation
  suspend fun Ids(@Name("ids") ids: Collection<String>): JsonArray = json {
    array { ids.forEach { add(it) } }
  }
}
